package com.dd.cloud.common.config.sms;

import com.dd.cloud.common.config.common.ProfilesProperties;
import com.dd.cloud.common.constants.ZekeConstants;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 短信工具类
 *
 * @Author: czg
 * @Date: 2022-04-08 10:15:15
 */
@Component
public class SmsComponent {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProfilesProperties profilesProperties;

    /**
     * 校验验证码
     *
     * @param mobileNumber
     * @param code
     */
    public void verifyPhoneCode(String mobileNumber, String code) {
        String cacheKey = ZekeConstants.CommonConstants.PHONE_KEY_PREFIX + mobileNumber;
        if (!ZekeConstants.ProfilesType.PROD.equals(profilesProperties.getActive()) && "194711".equals(code)) {
            redisTemplate.delete(cacheKey);
            return;
        }
        String cacheCode = redisTemplate.opsForValue().get(cacheKey);
        if (!StringUtils.equals(cacheCode, code)) {
            throw new ZekeException(ZkExceptionEnum.INVALID_VERIFY_CODE);
        }
        redisTemplate.delete(cacheKey);
    }
}
